//
//  BuyWindowStandardView.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/7/4.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "BuyWindowStandardView.h"

@implementation BuyWindowStandardView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setNeedsLayout{
    _buttonArray = [NSMutableArray array];
    
    CGFloat yFrame = 0;
    //规格
    UILabel *standard = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 40, 18)];
    standard.textColor = [UIColor grayColor];
    standard.font = [UIFont systemFontOfSize:16];
    NSString *key = _standardDic.allKeys[0];
    standard.text = [NSString stringWithFormat:@"%@", key];
    [self addSubview:standard];
    
    //规格的按钮数组
    CGFloat width = 0;
    CGFloat height = 0;
    //分类
    for (int j = 0; j < [[_standardDic valueForKey:key] count]; j++) {
        //规格文本
        NSString *string = [_standardDic valueForKey:key][j];
        UILabel *specificLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, yFrame, 0, 25)];
        specificLabel.backgroundColor = [UIColor clearColor];
        specificLabel.textColor = [UIColor grayColor];
        specificLabel.textAlignment = NSTextAlignmentCenter;
        specificLabel.text = [NSString stringWithFormat:@"%@", string];
        
        [specificLabel setFrame:CGRectMake(70 + width, height, 1000, 25)];
        [specificLabel sizeToFit];
        
        //选择规格按钮
        UIButton *specific = [UIButton buttonWithType:UIButtonTypeCustom];
        [specific setBackgroundImage:[UIImage imageNamed:@"detailBuyicon_10"] forState:UIControlStateNormal];
        specific.layer.cornerRadius = 4;
        specific.layer.masksToBounds = YES;
        specific.frame = CGRectMake(70 + width - 5, height - 1, specificLabel.frame.size.width + 10, 25);
        [specific addTarget:self action:@selector(chooseStandard:) forControlEvents:UIControlEventTouchUpInside];
        
        if (width + specific.frame.size.width > WIDTH - 72) {
            width = 0;
            height = height + 35;
        }
        
        specificLabel.frame = CGRectMake(70 + width, height, 1000, 25);
        [specificLabel sizeToFit];
        specific.frame = CGRectMake(70 + width - 5, height - 1, specificLabel.frame.size.width + 10, 25);
        
        [self addSubview:specific];
        [self addSubview:specificLabel];

        NSMutableArray *array = [NSMutableArray arrayWithObjects:specific, specificLabel, nil];
        
        //按钮加入数组中
        [_buttonArray addObject:array];
        
        width = width + specific.frame.size.width + 25;
        
        if (j == [[_standardDic valueForKey:key] count] - 1) {
            yFrame = specific.frame.origin.y + specific.frame.size.height + 20;
        }
    }
    _height = yFrame;
}

//选择规格
- (void)chooseStandard:(UIButton *)sender{
    for (NSMutableArray *arr in _buttonArray) {
        UIButton *button = arr[0];
        UILabel *label = arr[1];
        [button setBackgroundImage:[UIImage imageNamed:@"detailBuyicon_10"] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor darkGrayColor];
        if (sender == button) {
//            [button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            button.backgroundColor = APPCOLOR;
            label.textColor = [UIColor whiteColor];
            _chooseStandardString(label.text, _standardDic.allKeys[0]);
        }
    }
}

@end
